package ca.ubc.cpsc210.waldo.translink;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import ca.ubc.cpsc210.waldo.model.Bus;
import ca.ubc.cpsc210.waldo.model.BusRoute;
import ca.ubc.cpsc210.waldo.model.BusRoute;
import ca.ubc.cpsc210.waldo.model.BusStop;
import ca.ubc.cpsc210.waldo.util.LatLon;

public class BusEstimatesParser extends DefaultHandler {
	
	StringBuffer accumulator;
	
	String direction;		//Bus
	int minutesToDeparture;
	
	BusRoute route;
	BusStop stop;
	
	boolean cancelled;
	
	
	Bus bus;
	
	String number;			// BusRoute
	
	
	
	
	
	List<BusRoute> routes;
	
	BusStop stopC;
	
	
	public BusEstimatesParser(BusStop stopC, List<BusRoute> routes) {
		this.stopC = stop;
		this.routes = routes;
		
		direction = null;
		minutesToDeparture = 0;
		
		route = null;
		stop = null;
		
		cancelled = false;
		
		bus = null;
		
		number = null;
		
		routes = new ArrayList<BusRoute>();
		
	}
	
	/**
	 * Called at the start of the document (as the name suggests)
	 */
	@Override
	public void startDocument() {
		// Print out a message to let you know something is happening. This is
		// just to help you trace the program executing.
		System.out.println("Start Document!");

		// Use accumulator to remember information parsed. Just initialize for
		// now.
		accumulator = new StringBuffer();
		
		direction = null;
		minutesToDeparture = 0;
		
		route = null;
		stop = null;
		
		bus = null;
		
		cancelled = false;
		
		number = null;
	}
	
	/**
	 * Called when the parsing of an element starts. (e.g., <book>)
	 * 
	 * Lookup documentation to learn meanings of parameters.
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) {
		System.out.println("StartElement: " + qName);
		// What are we parsing?
		if (qName.toLowerCase().equals("nextbus")) {
//			direction = null;
			minutesToDeparture = 0;
			
			route = null;
			stop = null;
			
			cancelled = false;
			
			bus = null;
			
			number = null;
		} 
		// Let's start the accumulator
		// to remember the value that gets parsed
		accumulator.setLength(0);
	}
	
	/**
	 * Called for values of elements
	 * 
	 * Lookup documentation to learn meanings of parameters.
	 */
	public void characters(char[] temp, int start, int length) {
		// Remember the value parsed
		accumulator.append(temp, start, length);
	}
	
	/**
	 * Called when the end of an element is seen. (e.g., </title>)
	 * 
	 * Lookup documentation to learn meanings of parameters.
	 */
	public void endElement(String uri, String localName, String qName) {
		// Print out that we have seen the end of an element
		System.out.println("EndElement: " + qName + " value: " + accumulator.toString().trim());
		
		// Remember details about the book being parsed
		if (qName.toLowerCase().trim().equals("direction")) {
			direction = accumulator.toString().trim();
		} else if (qName.toLowerCase().trim().equals("expectedcountdown")) {
			minutesToDeparture = Integer.parseInt(accumulator.toString().trim());
		} else if (qName.toLowerCase().trim().equals("cancelledtrip")) {
			cancelled = Boolean.parseBoolean(accumulator.toString().trim());
		} else if (qName.toLowerCase().trim().equals("routeno")) {
			route = new BusRoute(accumulator.toString().trim());
		}
		
		
		if (qName.toLowerCase().equals("schedule")) {
			if (!cancelled) {
				bus = new Bus(route, direction, stop, minutesToDeparture);
				route.addBus(bus);
			}
		}
		
		if (qName.toLowerCase().equals("nextbus")) {
			routes.add(route);
		}
		
		// Reset the accumulator because we have seen the value
		accumulator.setLength(0);
	}
	
	/**
	 * Called when the end of the document is reached
	 */
	public void endDocument() {
		// Just let the user know as something to do
		
		System.out.println("End Document!");
	}

}
